import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import os
import warnings
warnings.filterwarnings('ignore')
pd.set_option('max_colwidth',200)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width', 1000)

root_path = os.path.abspath('../../')
data_path = root_path + "/asset/mergingData100m.csv"
ori_data = pd.read_csv(data_path)

mergingData = ori_data[
    (ori_data['MergingState']==True)
    & (ori_data['RouteClass']== 'entry')
    & (ori_data['BreakRuleState']=='None')
    & (ori_data['MergingDistance'] !='None')
    & (ori_data['MaxLateralSpeed'] !='None')
    & (ori_data['MergingType'] != 'None')
    & (ori_data['MergingType'] != 'G')
    & (ori_data['MergingType'] != 'H')
    ]

mergingData['miniTTC'] = mergingData.loc[:, ['MiniRearTTC', 'MiniLeadTTC']].min(axis=1)
mergingData = mergingData[mergingData["miniTTC"] < 15]

mergingData.sort_values(by="MergingType", inplace=True)

plt.figure(figsize=(10, 6))
ax1 = plt.subplot(111)
g1 = sns.boxplot(x="MergingType", y="miniTTC", data=mergingData[mergingData["location"] == 2],
                 hue="vehicleClass", showmeans=True, whis= 3,
                 meanprops={'marker': 'D', 'markerfacecolor': 'red'}, palette="hls",
                 saturation=0.6, showfliers=True)

plt.yticks([0,2,4,6,8,10,12,14,16,18,20])
g1.set(xlabel=None)
plt.ylabel("TTC(s)")
plt.grid(ls='-', axis="both")
sns.set_context("notebook")
plt.legend()
plt.show()
